package com.opennetexam.manager.utils.lock;

import com.opennetexam.manager.properties.JedisProperties;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @Description 获取RedissonClient连接类
 * @author 奶茶
 * Created by on 2019/2/20 9:48.
 */
@Configuration
@EnableConfigurationProperties(JedisProperties.class)
public class RedissonConnector {

    @Autowired
    private JedisProperties jedisProperties;

    RedissonClient redissonClient;

    @PostConstruct
    public void init(){
        Config config = new Config();
        config.useSentinelServers().addSentinelAddress(jedisProperties.getRedisNodes().split(","))
                .setMasterName(jedisProperties.getMaster())
                .setDatabase(jedisProperties.getDatabase())
                .setPassword(jedisProperties.getPassword())
                .setTimeout(jedisProperties.getTimeout());
        redissonClient = Redisson.create(config);
    }

    public RedissonClient getRedissonClient(){
        return redissonClient;
    }

}
